package com.southminority.ethnic.controller.organization;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.organization.OrganizationService;
import com.southminority.ethnic.controller.organization.vo.OrganizationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构组织展示控制器
 * 
 * @author SouthMinority
 */
@RestController
@RequestMapping("/api/organization")
@Tag(name = "12 - 机构组织展示API", description = "【前台】机构组织信息查询与展示相关接口")
public class OrganizationController {

    private final OrganizationService organizationService;

    public OrganizationController(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    @GetMapping("/homepage-list")
    @Operation(summary = "获取所有在首页展示的机构列表", description = "用于首页展示，结果按预设的首页排序值(`homepage_order`)升序排列。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<OrganizationVO>> getHomepageOrganizationList() {
        try {
            List<OrganizationVO> organizationList = organizationService.getHomepageOrganizationList();
            return R.ok(organizationList);
        } catch (Exception e) {
            return R.fail("获取首页机构列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/team-list")
    @Operation(summary = "获取所有在团队页展示的机构列表", description = "用于团队动态页的机构切换，仅返回启用且标记为团队展示的机构。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<OrganizationVO>> getTeamDisplayOrganizationList() {
        try {
            List<OrganizationVO> organizationList = organizationService.getTeamDisplayOrganizationList();
            return R.ok(organizationList);
        } catch (Exception e) {
            return R.fail("获取团队展示机构列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个机构的详细信息", description = "根据机构ID查询，返回其完整的公开信息。调用此接口会增加该机构的浏览次数。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "机构不存在或未启用"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<OrganizationVO> getOrganizationDetail(@Parameter(description = "机构的唯一ID", example = "1", required = true) @PathVariable Long id) {
        try {
            OrganizationVO organizationVO = organizationService.getOrganizationDetail(id);
            return R.ok(organizationVO);
        } catch (Exception e) {
            return R.fail("获取机构详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有已启用的机构列表", description = "用于机构列表页面，结果按通用排序值(`sort_order`)升序排列。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<OrganizationVO>> getOrganizationList() {
        try {
            List<OrganizationVO> organizationList = organizationService.getAllOrganizationList();
            return R.ok(organizationList);
        } catch (Exception e) {
            return R.fail("获取机构列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性", description = "一个简单的GET请求，用于检查服务是否正常运行。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "服务正常")
    })
    public R<String> test() {
        return R.ok("机构组织前端服务正常");
    }
} 
